﻿//+------------------------------------------------------------------------------------------------------------------------------------------------------
//
//Description: Json Pretty Printer.
//Author: Aravind Ramarathinam (aravr@microsoft.com)
//Copyright © Microsoft Corporation
//Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. 
//You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 
//
//THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, 
//INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT. 
//See the Apache Version 2.0 License for specific language governing permissions and limitations under the License.
// 
//--------------------------------------------------------------------------------------------------------------------------------------------------------

using System.Text;

namespace VmroleAuthor.Models
{
    public static class JsonHelper
    {
        public const string IndentSpace = "    ";
        public const string HalfIndentSpace = "  ";

        private static void AddIndentSpace(StringBuilder sb, int indent)
        {
            for (int i = 0; i < indent; ++i)
            {
                sb.Append(IndentSpace);
            }   
        }

        public static string PrettyPrentJson(string input)
        {
            if (string.IsNullOrEmpty(input))
            {
                return string.Empty;
            }
            StringBuilder sb = new StringBuilder();
            int indent = 1;
            bool insideStringLiteral = false;
            for (int i = 0; i < input.Length; ++i )
            {
                if (input[i] == '"')
                {
                    if (insideStringLiteral)
                    {
                        if (i > 0 && input[i - 1] != '\\')
                        {
                            insideStringLiteral = false;
                        }
                    }
                    else
                    {
                        insideStringLiteral = true;
                    }                  
                }
                if (insideStringLiteral)
                {
                    sb.Append(input[i]);
                }
                else
                {
                    if (input[i] == ',')
                    {
                        sb.Append(input[i]);
                        sb.AppendLine();
                        JsonHelper.AddIndentSpace(sb, indent);
                    }
                    else if (input[i] == '{' || input[i] == '[')
                    {
                        sb.Append(input[i]);
                        sb.AppendLine();
                        ++indent;
                        JsonHelper.AddIndentSpace(sb, indent);
                    }
                    else if (input[i] == ':')
                    {
                        sb.Append(input[i]);
                        sb.Append(HalfIndentSpace);
                    }
                    else if (input[i] == '}' || input[i] == ']')
                    {
                        sb.AppendLine();
                        --indent;
                        JsonHelper.AddIndentSpace(sb, indent);
                        sb.Append(input[i]);
                    }
                    else
                    {
                        sb.Append(input[i]);
                    }
                }
            }

            return sb.ToString();
        }

    }
}